package com.zsyh.jiujiu.fragment.orderFragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.DizhiBean;
import com.zsyh.jiujiu.dialog.ClallBackData;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.StatusUtil;
import com.zsyh.jiujiu.utils.TestAddressProvider;
import com.zsyh.jiujiu.weight.Adressxuanze;
import com.zsyh.jiujiu.weight.DiquSelect;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.AddressSelector;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AddAdressFragment extends DialogFragment implements ClallBackData, OnAddressSelectedListener {

    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.queding)
    TextView queding;
    @BindView(R.id.rvlist)
    FrameLayout rvlist;
    private Unbinder unbinder;
    private ClallBackData clallBackData;
    private String diqu;

    AppCompatActivity mContext;
    public AddAdressFragment(AppCompatActivity mContext){
        this.mContext=mContext;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.diaolg_select, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        unbinder = ButterKnife.bind(this, view);
        AddressSelector selector = new AddressSelector(getActivity());
        selector.setOnAddressSelectedListener(this);
        selector.setAddressProvider(new AddressProvider() {
            @Override
            public void provideProvinces(AddressReceiver<Province> addressReceiver) {
                sheng("",addressReceiver);
            }

            @Override
            public void provideCitiesWith(int i, AddressReceiver<City> addressReceiver) {

                chengshi(i+"",addressReceiver);
            }

            @Override
            public void provideCountiesWith(int i, AddressReceiver<County> addressReceiver) {

            }

            @Override
            public void provideStreetsWith(int i, AddressReceiver<Street> addressReceiver) {

            }
        });
        rvlist.addView(selector.getView());
        return view;
    }

    public void setSelectAddresFinish(ClallBackData clallBackData) {
        this.clallBackData = clallBackData;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels - StatusUtil.getStatusBarHeight(getActivity()));
    }


    @Override
    public void getData(String data, String name, String number) {

    }

    @OnClick({R.id.quxiao, R.id.queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quxiao:
                dismiss();
                break;
            case R.id.queding:
                clallBackData.getData(diqu,"","");
                dismiss();
                break;
        }
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        Log.e("dizhi",province.name+city.name);
        diqu=province.name+city.name+county.name;
    }

    /**
     * 获取省份
     */
    public void sheng(String id, AddressProvider.AddressReceiver<Province> addressReceiver){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        List<Province> list=new ArrayList<>();
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            Province province = new Province();
                            province.id = Integer.valueOf(dizhiBean.getData().get(i).getId());
                            province.name = dizhiBean.getData().get(i).getAreaName();
                            list.add(province);
                        }
                        addressReceiver.send(list);

                    }
                });
    }


    /**
     * 获取城市
     */
    public void chengshi(String id, AddressProvider.AddressReceiver<City> addressReceiver){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        List<City> list=new ArrayList<>();
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            City city = new City();
                            city.id = Integer.valueOf(dizhiBean.getData().get(i).getId());
                            city.name = dizhiBean.getData().get(i).getAreaName();
                            list.add(city);
                        }
                        addressReceiver.send(list);

                    }
                });
    }
}
